<?php

/*
function ThisPage()
{
    return $_SERVER['PHP_SELF'];
}
*/
function ActiveUrl()
{
    return $_SERVER['PHP_SELF'];
}

function h( $str )
{
    return htmlentities( $str, ENT_QUOTES ) ;
}

function UrlSelf()
{
    return $_SERVER['PHP_SELF'];
}

function self_url()
{
    return $_SERVER['PHP_SELF'];
}

function ReferringUrl()
{
    return $_SERVER['REQUEST_URI'];
}

function build_path($path, $filename)
{
    if(substr($filename, 0, 1) == '/') {
        $filename = substr($filename, 1, 1000);
    }

    if(substr($path, -1, 1) == '/') {
        $ret = $path . $filename;
    } else {
        $ret = $path . '/' . $filename;
    }
    return $ret;
}

function htmlEncode($value)
{
  return htmlspecialchars($value, ENT_QUOTES);
}

function htmlComment($str)
{
    return "<!--\n $str \n-->\n";
}

function PrettySql( $sql )
{
    return str_replace( "\n", "<br />\n", $sql ) ;
}

function SqlEncode($str)
{
    return mysql_real_escape_string($str);
}

function SqlQuote($str)
{
    return "'$str'";
}

function HTTPRequest($val)
{
    return @$_REQUEST[$val];
}

function days_between($earlierdate, $laterdate) 
{
    return ($laterdate - $earlierdate) / 24 / 60 / 60 ;
}

function y_m_d()
{
    return date('Y-m-d');
}

function pretty_date_time($ts)
{
    if(empty($ts))
        return "--" ;
    else
    return strftime("%c", $ts) ;
}

function MaybeMakeFolder($path)
{
    if(! file_exists($path)) {
        mkdir($path, 0777);
    }
}
function debugDump( $thing )
{
    echo "<br />";
    var_dump( $thing ) ;
    echo "<br />";
}

function debugEcho( $msg )
{
    echo "<br />$msg<br />\n";
}

/*
function now()
{
    return new DpDate();
}

function today()
{
    return new DpDate();
}
*/

function Arg($code)
{
    if( isset( $_GET[$code] ) )
        return $_GET[$code] ;
    if( isset( $_POST[$code] ) )
        return $_POST[$code] ;
    if( isset( $_SESSION[$code] ) )
        return $_SESSION[$code] ;
    return "" ;
}

// for SQL expressions
function QuoteOrNull($code)
{
    return empty($code) ? "NULL" : "'$code'" ;
}

function NumericNull($val)
{
    return empty($val) ? "NULL" : $val ;
}

// each link should be format "href|caption"
function EchoNavLinks( $div_class="", $ul_class, $li_class, $li_array ) {
    $ret = "";
    $ret .= $div_class 
        ? "<div>\n"
        : "<div class='{$ul_class}'>\n";
    $ret .= "
        <ul class='{$ul_class}'>\n";
    foreach($li_array as $li) {
        $_ary = explode("|", $li);
        $ret .= "
            <li class='{$li_class}'>
            <a href='{$_ary[0]}'>{$_ary[1]}</a>\n";
    }
    $ret .= "
        </ul>
    </div>\n";
    return $ret;
}
?>
